﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace PHTShop
{
    public partial class Template : System.Web.UI.MasterPage
    {
        private DataContextDataContext dc = new DataContextDataContext();
        private string _Title;

        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        private string _strUrl;

        public string strUrl
        {
            get { return _strUrl; }
            set { _strUrl = value; }
        }

        protected void menuClick()
        {
            string url=Request.Url.LocalPath;
            url=url.Substring(1,url.Length-6).ToLower();
            Default.Attributes.Remove("class");
            switch (url)
            { 
                case "about":
                    About.Attributes.Add("class","selected");
                    break;
                case "myaccount":
                    Myaccount.Attributes.Add("class","selected");
                    break;
                case "category":
                    Category.Attributes.Add("class","selected");
                    break;
                //case "details":
                //    Details.Attributes.Add("class", "selected");
                //    break;
                case "specials":
                    Specials.Attributes.Add("class", "selected");
                    break;
                case "register":
                    Register.Attributes.Add("class", "selected");
                    break;
                case "contact":
                    Contact.Attributes.Add("class", "selected");
                    break;
                default:
                    Default.Attributes.Add("class", "selected");
                    break;
            }
        }

        private void linkbuttonClick()
        {
            lbtnEN.Attributes.Remove("class");
            lbtnDE.Attributes.Remove("class");
            lbtnFR.Attributes.Remove("class");
            switch (Session["languages"].ToString())
            {
                case "Germany":
                    lbtnDE.Attributes.Add("class", "selected");
                    break;
                case "France":
                    lbtnFR.Attributes.Add("class", "selected");
                    break;
                default:
                    lbtnEN.Attributes.Add("class", "selected");
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                if (Session["Account"] != null)
                {
                    this.setAccount();
                    this.LinkButtonlogout.Text = "Logout";
                    this.LabelBr.Text = "<br /> <br />";
                    this.LinkButtonlogout.Visible = true;
                    this.LabelBr.Visible = true;
                }

                if (Session["languages"] == null)
                {
                    Session["languages"] = "en";
                }
                this.CategoryLanguage();
                this.PartnersLanguage();
                this.PromotionsLanguage();
                this.loadCart();
                this.loadAboutUs();
              
            }
            linkbuttonClick();
            menuClick();
            Details.Visible = false;
        }


        private void CategoryLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            var list = dc.CategoriesLanguages.Join(dc.Categories,
                       cl => cl.categoriesId,
                       c => c.id,
                       (cl, c) => new
                       {
                           status = c.status,
                           oders = c.orders,
                           name = cl.name,
                           languageId = cl.languageId,
                           categoriesId = cl.categoriesId,
                       }
                );

            this.RepeaterCategories.DataSource = list.Where(t => t.languageId == lg.id && t.status == true).OrderBy(t => t.oders).Skip(0).Take(7);
            this.RepeaterCategories.DataBind();
        }

        private void PartnersLanguage()
        {
            this.RepeaterPartners.DataSource = this.dc.Partners.ToList().Skip(0).Take(7);
            this.RepeaterPartners.DataBind();
        }

        private void PromotionsLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            List<Product> products = this.dc.Products.Where(t => t.status == true && t.discount > 0).ToList();

            List<ClassLoadPromotions> list = new List<ClassLoadPromotions>();

            foreach (var i in products)
            {
                Product pro = this.dc.Products.Single(t => t.id == i.id);
                ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.productsId == i.id && t.languageId == lg.id);
                string photoName = "";
                List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == i.id).ToList();
                foreach (var j in productsPhoto)
                {
                    ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                    photoName = propt.name;

                }

                list.Add(new ClassLoadPromotions(pro, prolg, photoName));
            }

            this.RepeaterPromotions.DataSource = list.Take(3);
            this.RepeaterPromotions.DataBind();
        }

        private void setAccount()
        {
            Account ac = Session["Account"] as Account;
            this.Labelaccount.Text = "Welcome, " + ac.fullname + " !";

        }

        protected void LinkButtonlogout_Click(object sender, EventArgs e)
        {
            Session.Remove("Account");

            this.Labelaccount.Text = string.Empty;
            this.LabelBr.Text = string.Empty;
            this.Labelaccount.Visible = false;
            this.LabelBr.Visible = false;
            Response.Redirect("Default.aspx");
        }

        private void loadCart()
        {
            List<Items> cart = (List<Items>)Session["Cart"];
            if (cart != null)
            {
                this.Labelitems.Text = cart.Count.ToString();
                this.Labeltotal.Text = this.total().ToString();
            }
            else
            {
                this.Labelitems.Text = "0";
                this.Labeltotal.Text = "0";
            }
        }

        private decimal total()
        {
            decimal s = 0;
            List<Items> cart = (List<Items>)Session["Cart"];
            foreach (Items it in cart)
                s += it.pro.price.Value * it.qua;
            return s;
        }

        private void loadAboutUs()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));
            Content about = this.dc.Contents.Single(t => t.name.Equals("about") && t.idlanguage == lg.id);
            string strAbout="";
            if (about.details.Length >= 200)
                strAbout = about.details.Substring(0, 200);
            else
                strAbout = about.details;
            this.LiteralaboutUs.Text = strAbout;
        }

        protected void lbtnEN_Click(object sender, EventArgs e)
        {
            Session["languages"] = "English";
            Response.Redirect(Request.Url.AbsoluteUri);
        }
        protected void lbtnFR_Click(object sender, EventArgs e)
        {
            Session["languages"] = "France";
            Response.Redirect(Request.Url.AbsoluteUri);
        }
        protected void lbtnDE_Click(object sender, EventArgs e)
        {
            Session["languages"] = "Germany";
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}